package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserCharitableSkill;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/9/1
 */
public class UserCharitableSkillDaoImpl extends TempDataObjectDao<UserCharitableSkill> {

    public static String getUserCharitableSkillSql = "select * from t_u_charitable_skill where userId=?";

    public Map<Integer, Map<Integer, UserCharitableSkill>> getUserCharitableSkillMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, UserCharitableSkill>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserCharitableSkillSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCharitableSkill userCharitableSkill = parse(rs);
                if (!map.containsKey(userCharitableSkill.getPosition())) {
                    map.put(userCharitableSkill.getPosition(), new ConcurrentHashMap<>());
                }
                map.get(userCharitableSkill.getPosition()).put(userCharitableSkill.getSkillId(), userCharitableSkill);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    public static String updateUserCharitableSkillSql = "update t_u_charitable_skill set level=? where userId=? and position=? and skillId=?";

    @Override
    protected boolean updateData(UserCharitableSkill userCharitableSkill) {
        userCharitableSkill.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userCharitableSkill.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userCharitableSkill.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitableSkill.getPosition()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitableSkill.getSkillId()));
        if (getDb().executeNoneQuery(updateUserCharitableSkillSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserCharitableSkillSql = "insert into t_u_charitable_skill(userId,position,skillId,level) values (?,?,?,?)";

    @Override
    protected boolean addData(UserCharitableSkill userCharitableSkill) {
        userCharitableSkill.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCharitableSkill.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitableSkill.getPosition()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitableSkill.getSkillId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitableSkill.getLevel()));
        if (getDb().executeNoneQuery(insertUserCharitableSkillSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserCharitableSkill parseData(ResultSet rs) throws SQLException {
        UserCharitableSkill userCharitableSkill = new UserCharitableSkill();
        userCharitableSkill.setUserId(rs.getLong("userId"));
        userCharitableSkill.setPosition(rs.getInt("position"));
        userCharitableSkill.setSkillId(rs.getInt("skillId"));
        userCharitableSkill.setLevel(rs.getInt("level"));

        return userCharitableSkill;
    }
}
